package com.netsdk.demo.customize;

import com.netsdk.demo.util.CaseMenu;
import com.netsdk.lib.NetSDKLib;
import com.netsdk.lib.PlaySDKLib;
import com.netsdk.lib.ToolKits;
import com.netsdk.lib.structure.NET_IN_ATTACH_TRAFFIC_FLOW_STAT_REAL_FLOW;
import com.netsdk.lib.structure.NET_OUT_ATTACH_TRAFFIC_FLOW_STAT_REAL_FLOW;
import com.netsdk.lib.structure.NET_VEHICLE_INOUT_ANALYSE_PROC;
import com.netsdk.lib.structure.NET_VEHICLE_OBJECT;
import com.netsdk.lib.utils.Initialization;
import com.sun.jna.Pointer;

public class TrafficFlowDemo  extends Initialization {


    /**变量区**/
    int channel=0;
    NetSDKLib.LLong attachHandle = new NetSDKLib.LLong(0);
    static NetSDKLib netsdkApi = NetSDKLib.NETSDK_INSTANCE;
    static NetSDKLib configApi = NetSDKLib.CONFIG_INSTANCE;
    static PlaySDKLib playsdkApi   = PlaySDKLib.PLAYSDK_INSTANCE;

    /**方法区**/

    private static class VehicleInOutAnalyseProc implements NetSDKLib.fVehicleInOutAnalyseProc{

        private VehicleInOutAnalyseProc() {
        }

        private static class VehicleInOutAnalyseHolder {
            private static final VehicleInOutAnalyseProc instance = new VehicleInOutAnalyseProc();
        }

        public static VehicleInOutAnalyseProc getInstance() {

            return VehicleInOutAnalyseProc.VehicleInOutAnalyseHolder.instance;
        }
        @Override
        public void invoke(NetSDKLib.LLong lAttachHandle, Pointer pstuVehicleInOutAnalyseProc, Pointer dwUser) {
            if (lAttachHandle.longValue() == 0 || pstuVehicleInOutAnalyseProc == null) {
                return;
            }
            NET_VEHICLE_INOUT_ANALYSE_PROC cbMsg = new NET_VEHICLE_INOUT_ANALYSE_PROC();
            ToolKits.GetPointerData(pstuVehicleInOutAnalyseProc, cbMsg);

            System.out.println("==========CallBack received==========");
            System.out.println("szName = " + new String(cbMsg.szName).trim());
            System.out.println("dbPTS = " + cbMsg.dbPTS);
            System.out.println("stuUTC = " + cbMsg.stuUTC.toStringTime());
            System.out.println("nEventID = " + cbMsg.nEventID);
            System.out.println("nSequence = " + cbMsg.nSequence);
            System.out.println("nFrameSequence = " + cbMsg.nFrameSequence);
            System.out.println("dbRadarInstallAngle = " + cbMsg.dbRadarInstallAngle);
            System.out.println("nStatNum = " + cbMsg.nStatNum);
            for(int i = 0; i < cbMsg.nStatNum; i ++){
                System.out.println("stuStats[" + i + "] = " + cbMsg.stuStats[i].toString());
            }
            System.out.println("nObjectNum = " + cbMsg.nObjectNum);
            if(cbMsg.nObjectNum > 0){
                NET_VEHICLE_OBJECT[] stuObject = new NET_VEHICLE_OBJECT[cbMsg.nObjectNum];
                for(int i = 0; i < cbMsg.nObjectNum; i ++){
                    stuObject[i] = new NET_VEHICLE_OBJECT();
                }
                ToolKits.GetPointerDataToStructArr(cbMsg.pstuObjets, stuObject);
                for(int i = 0; i < cbMsg.nObjectNum; i ++){
                    System.out.println("pstuObject[" + i + "] = " + stuObject[i].toString());
                }
            }
        }
    }
    
    
    /**
     * 订阅交通流量统计
     */
    public void AttachTrafficFlowStateRealFlow() {
        //先关闭，再开启
        if(attachHandle.intValue() != 0){
            DetachTrafficFlowStateRealFlow();
        }
        NET_IN_ATTACH_TRAFFIC_FLOW_STAT_REAL_FLOW stIn = new NET_IN_ATTACH_TRAFFIC_FLOW_STAT_REAL_FLOW();
        stIn.cbVehicleInOutAnalyseProc = VehicleInOutAnalyseProc.getInstance();
        NET_OUT_ATTACH_TRAFFIC_FLOW_STAT_REAL_FLOW stOut = new NET_OUT_ATTACH_TRAFFIC_FLOW_STAT_REAL_FLOW();
        stIn.write();
        stOut.write();
        attachHandle = netsdkApi.CLIENT_AttachTrafficFlowStatRealFlow(loginHandle, stIn.getPointer(), stOut.getPointer(), 3000);
        if(attachHandle.intValue() != 0){
            System.out.println("AttachTrafficFlowStatRealFlow success");
        } else {
            System.err.println("AttachTrafficFlowStatRealFlow failed" + ToolKits.getLastError());
        }
    }

    public void DetachTrafficFlowStateRealFlow() {
        if(netsdkApi.CLIENT_DetachTrafficFlowStatRealFlow(attachHandle)){
            System.out.println("DetachTrafficFlowStatRealFlow success");
        } else {
            System.err.println("DetachTrafficFlowStatRealFlow failed" + ToolKits.getLastError());
        }
    }

    public void RunTest()
    {
        System.out.println("Run Test");
        CaseMenu menu = new CaseMenu();;
        menu.addItem((new CaseMenu.Item(this , "AttachTrafficFlowStateRealFlow" , "AttachTrafficFlowStateRealFlow")));
        menu.addItem((new CaseMenu.Item(this , "DetachTrafficFlowStateRealFlow" , "DetachTrafficFlowStateRealFlow")));
        menu.run();
    }

    public static void main(String[] args) {
        TrafficFlowDemo demo = new TrafficFlowDemo();
        /**登录信息运行时输入**/
//        Scanner sc = new Scanner(System.in);
//        System.out.print("ip:");
//        String ip = sc.nextLine();
//        System.out.print("port:");
//        String tmp = sc.nextLine();
//        int port = Integer.parseInt(tmp);
//        System.out.print("username:");
//        String username = sc.nextLine();
//        System.out.print("password:");
//        String pwd = sc.nextLine();
//        InitTest(ip,port,username,pwd);
        /**登录信息固定**/
        InitTest("172.9.102.188",37777,"admin","admin123");
        demo.RunTest();
        LoginOut();

    }
}
